<template>
  <j-modal title="转诊信息填写" :width="900" :visible="visible" :confirmLoading="loading" switchFullscreen
    @cancel="handleCancel" @ok="handleOk">
    <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol"
      style="padding:0 0 20px 20px;">
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="原治疗地区" required prop="regionArr">
            <z-linkage-area placeholder="请选择行政区划" v-model="form.regionArr" :disabled="isDisabled" @rows="onRows1"/>
            <!-- <a-input v-model="form.originRegion" :disabled="isDisabled" placeholder="请输入"> </a-input> -->
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="原治疗门诊" required prop="originClinic">
            <a-input v-model="form.originClinic" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="拟转诊至" prop="targetRegionArr">
            <z-linkage-area placeholder="请选择行政区划" v-model="form.targetRegionArr" @change="setAdminDivisions"
              @rows="onRows2" />
            <!-- <a-input v-model="form.targetRegion" placeholder="请输入"> </a-input> -->
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="治疗门诊" prop="targetClinic">
            <a-select v-model="form.targetClinic" placeholder="请选择" allowClear>
              <a-select-option v-for="item in clinicNameArr" :key="item">{{ item }}
              </a-select-option>
            </a-select>
            <!-- <a-input v-model="form.targetClinic" placeholder="请输入"> </a-input> -->
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="病人编号" prop="patientId">
            <a-input v-model="form.patientId" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="转诊申请时间" prop="referralApplyTime">
            <a-date-picker allowClear v-model="form.referralApplyTime" valueFormat="YYYY-MM-DD" style="width:100%" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="转诊级别" prop="referralLevel">
            <a-input v-model="form.referralLevel" :disabled="isDisabled" placeholder="请输入"> </a-input>
            <!-- <a-select v-model="form.referralLevel" placeholder="请选择">
              <a-select-option value="0">省内转诊</a-select-option>
              <a-select-option value="1">省外转诊</a-select-option>
            </a-select> -->
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="转诊事由概要" prop="referralCause">
            <a-input v-model="form.referralCause" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="治疗建议" required prop="cure_proposal">
            <a-input v-model="form.cure_proposal" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="现维持剂量" required prop="curDose">
            <a-input-number v-model="form.curDose" placeholder="请输入" style="width:92%;"> </a-input-number> ml
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="转诊时间" prop="rangeTime">
            <a-range-picker allowClear v-model="form.rangeTime" valueFormat="YYYY-MM-DD" @change="rangePickerChange"
              style="width:100%" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="长期变更服药门诊" required prop="isChange">
            <a-select v-model="form.isChange" placeholder="请选择" allowClear>
              <a-select-option value="0">否</a-select-option>
              <a-select-option value="1">是</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </j-modal>
</template>
<script>
import * as treatApi from '@/api/sjsk/help/treat.js'
export default {
  components: {},
  data() {
    return {
      labelCol: { span: 22 },
      wrapperCol: { span: 22 },
      form: {},
      rules: {
        regionArr: [{ required: true, message: '请输入' }],
        originClinic: [{ required: true, message: '请输入' }],
        targetRegionArr: [{ required: true, message: '请输入' }],
        targetClinic: [{ required: true, message: '请输入' }],
        patientId: [{ required: true, message: '请输入' }],
        referralApplyTime: [{ required: true, message: '请选择' }],
        referralLevel: [{ required: true, message: '请选择' }],
        referralCause: [{ required: true, message: '请输入' }],
        cure_proposal: [{ required: true, message: '请输入' }],
        curDose: [{ required: true, message: '请输入' }],
        rangeTime: [{ required: true, message: '请选择' }],
        isChange: [{ required: true, message: '请选择' }],
      },
      isDisabled: true,
      visible: false,
      loading: false,
      clinicNameArr: [],
    }
  },
  created() {
    // let user = Vue.ls.get(USER_INFO)
    // this.form.apprehendUser = Vue.ls.get(USER_INFO).realname
  },
  methods: {
    show(record) {
      this.visible = true
      this.form = {}
      this.clinicNameArr = []
      this.$set(this.form, 'patientId', record.patientId)
      this.$set(this.form, 'referralLevel', "省内转诊")
      this.$set(this.form, 'originRegion', record.originRegion)
      this.$set(this.form, 'regionArr', record.originRegion.split(','))
      this.$set(this.form, 'originClinic', record.originClinic)
      // this.getDetail(patientId)
    },
    getDetail(patientId) {
      treatApi.getClinicDetailsByPid({ patientId: patientId }).then(res => {
        if (res.success) {
          this.$set(this.form, 'originRegion', res.result.originRegion)
          this.$set(this.form, 'originClinic', res.result.originClinic)
        }
      })
    },
    // 获取行政区划的值
    setAdminDivisions(value) {
      this.form.targetRegion = value.join()
      // console.log(this.form.region)
      treatApi.getClinicNameByRegion({ region: this.form.targetRegion }).then((res) => {
        if (res.success) {
          this.clinicNameArr = res.result
        }
      })
    },
    //原来区域
    onRows1(row) {
      // console.log("value")
      // console.log(row)
      let str = ""
      row.forEach(item => {
        str += item.title + "," //保存行政区划名称
      })
      this.form.originRegionWords = str.slice(0, str.length - 1) //删除最后一个逗号
      // console.log(this.form.interviewPlace)
    },
    //目的地区域
    onRows2(row) {
      // console.log("value")
      // console.log(row)
      let str = ""
      row.forEach(item => {
        str += item.title + "," //保存行政区划名称
      })
      this.form.targetRegionWords = str.slice(0, str.length - 1) //删除最后一个逗号
      // console.log(this.form.interviewPlace)
    },
    rangePickerChange(date) {
      this.$set(this.form, 'startTime', date[0])
      this.$set(this.form, 'endTime', date[1])
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          treatApi.addReferralByPid(this.form).then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok');
              this.visible = false
              this.loading = false
            } else {
              this.loading = false
              this.$message.warn(res.message)
            }
          })
            .catch(e => {
              this.loading = false
              this.$message.warn('请求失败！')
            })
        }
      })
    }
  }
}
</script>
<style scoped lang="less">
/deep/.ant-col-22 {
  text-align: left;
}

/deep/.ant-row {
  margin-bottom: 0;
}

/deep/.ant-space-item {
  flex: none;
  width: 119px;
}
</style>
